/**
 * 菜单管理.
 */
var curtable;

$(document).ready(function() {
 
    //初始化菜单列表
    initTable();
    //$("#spinner").spinner();

	// 数据删除
	$('#roleTable tbody').on('click', 'A#btnEdit', function() {
		var data = curtable.row($(this).parents('tr')).data();
		alert(data);
		/*
		 * $.ajax({ url: 'deltable', type: 'POST', dataType:
		 * 'json', data: {id: data[0]}, }) .done(function(data) {
		 * if (data=="success") { tables.ajax.reload(); }; })
		 * .fail(function() { alert("error"); });
		 */
	});
	// 数据编辑
	$('#roleTable tbody').on('click', 'A#btnDel', function() {
		var data = curtable.row($(this).parents('tr')).data();
		alert(data);
		/*
		 * var fields = $("#add-form").serializeArray();
		 * jQuery.each( fields, function(i, field){
		 * //jquery根据name属性查找
		 * $(":input[name='"+field.name+"']").val(data[i]); });
		 * $(":input[name='mark']").val("edit");
		 * $("#modal-form").modal("show");//弹出框show
		 */
	});
	$("#btnSearch").click(function() {
		curtable.ajax.reload();
	})
});
function initTable() {
    var table = $('#roleTable');
    curtable = table.DataTable($.extend({ // old API: $().dataTable()
        // returns jQuery object, so you
        // should use .api() in order to
        // use DataTable API methods:
        // oTable.api().ajax.reload();
        // "order": [ 3, "desc" ],
        "bSort": false,
        ajax: function(data, callback) {
            $.ajax({
                url: basePath + "/sys/sysrole/ajaxGetListPage.shtml",
                type: "post",
                data: JSON.stringify(getQueryCondition(data)),
                contentType: "application/json; charset=UTF-8",
                dataType: "json",
                success: function(jsonResult) {
                    if (jsonResult.success) {
                        // 封装返回数据，这里仅演示了修改属性名
                        var returnData = {};
                        // returnData.draw
                        // =8;//
                        // data.draw;//这里直接自行返回了draw计数器,应该由后台返回
                        returnData.iTotalDisplayRecords = jsonResult.iTotalDisplayRecords;
                        // returnData.sEcho=2;
                        returnData.data = jsonResult.data;
                        returnData.iTotalRecords = jsonResult.iTotalRecords;
                        callback(returnData);

                    } else {
                        Metronic.alert({
                            type: (jsonResult.success == true ? 'success': 'danger'),
                            icon: (jsonResult.success == true ? 'check': 'warning'),
                            message: jsonResult.errorMessages[0].msg,
                            container: tableWrapper,
                            place: 'prepend'
                        });
                        //curtable.Table.Rows.Clear()
                        curtable = null;
                        // curtable.Rows.Clear();
                    }
                },
                // 请求失败
                error: function(xhr) {

              }
            });
        },
        "columns": [{ // 复选框单元格
            "className": "td-checkbox",
            "data": "id",
            "orderable": false,
            "width": "30px",
            "render": function(data, type, row, meta) {
                return '<input type="checkbox" value=' + data + ' class="iCheck">';
            }
        },
        {
            "data": "roleCode",
            "name": "roleCode",
            "orderable": false
        },
        {
            "data": "roleName",
            "name": "roleName",
            "orderable": false
        },
        {
            "data": "roleType",
            "name": "roleType",
            "orderable": false
        },
        {
            "data": "officeId",
            "name": "officeId",
            "orderable": false
        },
        {
            "data": "useable",
            "name": "useable",
            "orderable": false,
            render: function(data, type, row, meta) {
                if (data == '1') {
                    return '是';
                } else {
                    return '否';
                }

            }
        },
        {
            "data": null,
            "width": "80px",
            "defaultContent": "",
            "orderable": false
        }],
        // 在最后一列增加编辑删除按钮
        "createdRow": function(row, data, index) {
            // 行渲染回调,在这里可以对该行dom元素进行任何操作
            $('td', row).eq(6).addClass("taC");
            var btnEdit = $('<div class="userM-table-function"><a id="btnEdit" class="fa fa-pencil-square text-success"></a> <a id="btnDel" class="fa fa-trash text-danger"></a></div>');
            $('td', row).eq(6).append(btnEdit);
        }
    },
    CONSTANT.DATA_TABLES.DEFAULT_OPTION));
}
 
 
function getQueryCondition(data) {
	var param = {};
	// 组装排序参数
	/*
	 * if (data.order&&data.order.length&&data.order[0]) { switch
	 * (data.order[0].column) { case 1: param.orderColumn = "name"; break; case
	 * 2: param.orderColumn = "position"; break; case 3: param.orderColumn =
	 * "status"; break; case 4: param.orderColumn = "start_date"; break;
	 * default: param.orderColumn = "name"; break; } param.orderDir =
	 * data.order[0].dir; }
	 */
	// 组装查询参数
	// param.fuzzySearch = userManage.fuzzySearch;
	/*
	 * if (userManage.fuzzySearch) { param.fuzzy = $("#fuzzy-search").val();
	 * }else{ param.name = $("#name-search").val(); param.position =
	 * $("#position-search").val(); param.office = $("#office-search").val();
	 * param.extn = $("#extn-search").val(); param.status =
	 * $("#status-search").val(); param.role = $("#role-search").val(); }
	 */
	// 组装分页参数
	param.STARTINDEX = data.start;
	param.PAGESIZE = data.length;
	//param.name = $("#name").val();
	return param;
}
/*$('#menuTable').on('order.dt',
	    function() {
	        alert('排序');
	    }).on('search.dt',
	    function() {
	    	alert('搜索');
	    }).on('page.dt',
	    function() {
	    	alert('翻页');
	    }).dataTable();*/
